package cn.zooz.common.result;


import java.io.Serializable;

/**
 * Created by Bradish7Y on 2017/9/23.
 * server tier 通用返回结果
 */
public class Result<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = -7319456660948335712L;

    private boolean success = false;
    private int code = -1;
    private String message = "";
    private T result;

    public Result() {

    }

    public Result(boolean success, int code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public Result(boolean success, int code, String message, T result) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public static <T extends Serializable> Result<T> newResult() {
        return new Result<>();
    }

    public static <T extends Serializable> Result<T> newResult(boolean success, int code, String message) {
        return new Result<>(success, code, message);
    }

    public static <T extends Serializable> Result<T> newResult(boolean success, int code, String message, T t) {
        return new Result<>(success, code, message, t);
    }

    public boolean isSuccess() {
        return success;
    }

    public Result setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public int getCode() {
        return code;
    }

    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getResult() {
        return result;
    }

    public Result setResult(T result) {
        this.result = result;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Result{");
        sb.append("success=").append(success);
        sb.append(", code=").append(code);
        sb.append(", message='").append(message).append('\'');
        sb.append(", result=").append(result);
        sb.append('}');
        return sb.toString();
    }
}
